﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DVDFilmoteka.src.Exporters
{
    public class TXTDatabaseExporter : IDatabaseExporter
    {
        private List<MovieStorage.MovieDataStore12> database = null;
        private MovieStorage.MovieDataStore12 columns = null;
        private String fileName = null;

        #region IDatabaseExporter Members

        public List<MovieStorage.MovieDataStore12> Database
        {
            get
            {
                return database;
            }
            set
            {
                database = value;
            }
        }

        public MovieStorage.MovieDataStore12 ColumnsToExport
        {
            get
            {
                return columns;
            }
            set
            {
                columns = value;
            }
        }

        public String FileName
        {
            get
            {
                return fileName;
            }
            set
            {
                fileName = value;
            }
        }

        public void Export()
        {
            if (database == null || columns == null || String.IsNullOrEmpty(fileName)) return;

            StreamWriter writer = new StreamWriter(fileName);

            String firstLine = "";
            if (columns.czechName != null) firstLine += "Český název\t";
            if (columns.originalName != null) firstLine += "Originální název\t";           

            if (columns.myRating != null) firstLine += "Moje hodnocení\t";
            if (columns.csfdRating != null) firstLine += "CSFD hodnocení\t";
            if (columns.imdbRating != null) firstLine += "IMDB hodnocení\t";

            if (columns.mainGenre != null) firstLine += "Žánr\t";
            if (columns.country != null) firstLine += "Země původu\t";
            if (columns.year != null) firstLine += "Rok\t";
            if (columns.length != null) firstLine += "Délka\t";
            if (columns.directorName != null) firstLine += "Režisér\t";            
            if (columns.recenze != null) firstLine += "Recenze\t";

            if (columns.medium != null) firstLine += "Medium\t";
            if (columns.compression != null) firstLine += "Komprese\t";
            if (columns.container != null) firstLine += "Kontejner\t";
            if (columns.quality != null) firstLine += "Kvalita\t";
            if (columns.resolution != null) firstLine += "Rozlišení\t";
            if (columns.size != null) firstLine += "Velikost\t";

            if (columns.language != null) firstLine += "Jazyk\t";
            if (columns.channelCount != null) firstLine += "Počet zvukových kanálů\t";
            
            if (columns.mediumNotes != null) firstLine += "Poznámky k médiu\t";
            if (columns.movieNotes != null) firstLine += "Poznámky k filmu\t";

            if (columns.link != null) firstLine += "Odkaz\t";

            writer.WriteLine(firstLine); // Zapise hlavicky sloupcu

            foreach (MovieStorage.MovieDataStore12 item in database)
            {
                String line = "";
                if (columns.czechName != null)
                {
                    if (item.czechName != null) { line += item.czechName + "\t"; } else { line += "\t"; }
                }

                if (columns.originalName != null)
                {
                    if (item.originalName != null) { line += item.originalName + "\t"; } else { line += "\t"; }
                }

                if (columns.myRating != null)
                {
                    if (item.myRating != null) { line += item.myRating + "\t"; } else { line += "\t"; }
                }
                if (columns.csfdRating != null)
                {
                    if (item.csfdRating != null) { line += item.csfdRating + "\t"; } else { line += "\t"; }
                }
                if (columns.imdbRating != null)
                {
                    if (item.imdbRating != null) { line += item.imdbRating + "\t"; } else { line += "\t"; }
                }

                if (columns.mainGenre != null)
                {
                    if (item.mainGenre != null) { line += item.mainGenre + "\t"; } else { line += "\t"; }
                }
                if (columns.country != null)
                {
                    if (item.country != null) { line += item.country + "\t"; } else { line += "\t"; }
                }
                if (columns.year != null)
                {
                    if (item.year != null) { line += item.year + "\t"; } else { line += "\t"; }
                }
                if (columns.length != null)
                {
                    if (item.length != null) { line += item.length + "\t"; } else { line += "\t"; }
                }
                if (columns.directorName != null)
                {
                    if (item.directorName != null) { line += item.directorName + "\t"; } else { line += "\t"; }
                }
                if (columns.recenze != null)
                {
                    if (item.recenze != null) { line += item.recenze + "\t"; } else { line += "\t"; }
                }

                if (columns.medium != null)
                {
                    if (item.medium != null) { line += item.medium + "\t"; } else { line += "\t"; }
                }
                if (columns.compression != null)
                {
                    if (item.compression != null) { line += item.compression + "\t"; } else { line += "\t"; }
                }
                if (columns.container != null)
                {
                    if (item.container != null) { line += item.container + "\t"; } else { line += "\t"; }
                }
                if (columns.quality != null)
                {
                    if (item.quality != null) { line += item.quality + "\t"; } else { line += "\t"; }
                }
                if (columns.resolution != null)
                {
                    if (item.resolution != null) { line += item.resolution + "\t"; } else { line += "\t"; }
                }
                if (columns.size != null)
                {
                    if (item.size != null) { line += item.size + "\t"; } else { line += "\t"; }
                }

                if (columns.language != null)
                {
                    if (item.language != null) { line += item.language + "\t"; } else { line += "\t"; }
                }
                if (columns.channelCount != null)
                {
                    if (item.channelCount != null) { line += item.channelCount + "\t"; } else { line += "\t"; }
                }

                if (columns.mediumNotes != null)
                {
                    if (item.mediumNotes != null) { line += item.mediumNotes + "\t"; } else { line += "\t"; }
                }
                if (columns.movieNotes != null)
                {
                    if (item.movieNotes != null) { line += item.movieNotes + "\t"; } else { line += "\t"; }
                }

                if (columns.link != null)
                {
                    if (item.link != null) { line += item.link + "\t"; } else { line += "\t"; }
                }

                writer.WriteLine(line);
            }

            writer.Close();
        }

        #endregion
    }
}
